package com.handy.chen.jiyu;



import java.io.IOException;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.kaloer.filepicker.FilePickerActivity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class Initialization  extends Activity implements constant,View.OnClickListener {
	 
	private Button single_button;
	private Button couple_button; 
	private MediaPlayer couple_music; 
	private Button couple_client;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.initial);
		// Create the adView  
        AdView adView = new AdView(this, AdSize.BANNER, "a15046e30963998");  
        LinearLayout layout = (LinearLayout) findViewById(R.id.adView);  
        // Add the adView to it  
        layout.addView(adView);  
        // Initiate a generic request to load it with an ad  
        adView.loadAd(new AdRequest());  
		
		
		
		try {
			init();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
 
	}
	/**
	 *  initialization process
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	private void init() throws IllegalStateException, IOException{
		 single_button  = (Button)findViewById(R.id.button1);
		 single_button.setOnClickListener(this);
		 
		 couple_client = (Button)findViewById(R.id.button_client); 
		 couple_client.setOnClickListener(this);
		
		 couple_button = (Button) findViewById(R.id.button4);
		 couple_button.setOnClickListener(this);
		 
		 couple_music = MediaPlayer.create(this, R.raw.my_heart); 
		 
		 try {
			 couple_music.prepare();
		 } catch (IllegalStateException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
		 } catch (IOException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
		 }
		 
		 couple_music.start();
		 
	}

	
	// check the result of setting the bluetooth
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    // use the result to check if the bluetooth is enabled 
		switch (resultCode){
		case RESULT_OK:
			// bluetooth is set 
			goto_setting_couple();
			
			break;
		case RESULT_CANCELED:
			// still not enabled
			Toast.makeText(Initialization.this, R.string.no_bluetooth_device, Toast.LENGTH_LONG).show();
			
			//exit 
			break;
		}
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.initial_setting, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.musicoff:
        	couple_music.stop();
        break;
        
        }
        return true;
    }
	
	public void onClick(View v) {
    	// TODO Auto-generated method stub
    	switch(v.getId()){
    	// show the result
    	
    	case (R.id.button1):
    	// goto single set
    		
    	Intent intent=new Intent();  
    	intent.setClass(Initialization.this, SettingWallpaper.class);  
		startActivity(intent);
		couple_music.stop();
    	
    	break;
    	    	
    	// bluetooth
    	case (R.id.button4):
    	
    		// goto couple set
    		// check bluetooth
    		couple_music.stop();
    			
    	    	Intent intent2=new Intent();  
    	    	intent2.setClass(Initialization.this, Setwp_cp.class );  
    			startActivity(intent2);
    			//goto_setting_couple();
    			
    			// goto_bluetooth_connection();setwallpaper
    		
    	break;
    	
    	case (R.id.button_client):
    		
    		couple_music.stop();
    		//start bluetooth setting to receive and set the wallpaper
    		Intent intent3=new Intent();  
    		intent3.setClass(Initialization.this, Set_Client.class );  
    		startActivity(intent3);
    		
    		
    		break; 		
       	}
    	
    }
	
	
	
	
	
	// helper functions ==================================================================================
	
	//check if the device is ready to use, return true if bluetooth is enabled
	public boolean is_good_to_go(){
		// check bluetooth
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    return false;
		} else {
		    if (!mBluetoothAdapter.isEnabled()) {
		        return false;
		    }
		}
		
		return true; 
	}
	
	// goto the next activity(search for active bluetooth device)
	public void goto_bluetooth_connection(){
		Intent intent=new Intent();  
		intent.setClass(Initialization.this, BluetoothConnection.class);  
		
		startActivity(intent);
		couple_music.stop();
		
	}
	
	private void goto_setting_couple(){
		Intent intent=new Intent();  
    	intent.setClass(Initialization.this, Setwp_cp.class);  
		startActivity(intent); 
		
		couple_music.stop();
		
	}
	
	
	// to enable the bluetooth device 
	public void enable_bluetooth(){
		// get the bluetooth adapter 
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		//set it 
		if (!mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		    
		}	
	}

		
	
}
